package com.factory.test.activity;

import android.os.Bundle;
import android.text.InputFilter;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.ScrollView;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.factory.test.base.BaseTestActivity;
import com.factory.test.R;
import com.factory.test.databinding.ActivityWiegandBinding;
import com.factory.test.util.DataConversion;
import com.yecontech.sdk.base.io.IOCallBack;
import com.yecontech.sdk.base.io.RS485Manager;
import com.yecontech.sdk.base.io.WiegandManager;
import com.yecontech.sdk.base.vo.Constants;

import java.util.Timer;
import java.util.TimerTask;


/**
 * 韦根测试
 */
public class WiegandActivity extends BaseTestActivity {

    private ActivityWiegandBinding binding;
    private Constants.WiegandBit wiegandBit = Constants.WiegandBit.WIEGAND26;
    private WiegandManager wiegandManager;

    private RS485Manager rs485Manager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_wiegand);
        wiegandManager = sdkService.getWiegandManager();
        wiegandManager.open(wiegandBit, new IOCallBack() {
            @Override
            public void onReceiverData(byte[] by) {
                appendMessage(DataConversion.toHexString(by, by.length));
            }
        });

        rs485Manager = sdkService.getRS485Manager();
        rs485Manager.open(new IOCallBack() {
            @Override
            public void onReceiverData(byte[] by) {
                appendMessage(DataConversion.toHexString(by, by.length));
            }
        });

        binding.groupBit.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId == R.id.bit26){
                    wiegandBit = Constants.WiegandBit.WIEGAND26;
                    binding.input.setFilters(new InputFilter[] { new InputFilter.LengthFilter(6)});
                    binding.input.setText("000001");
                }else if(checkedId == R.id.bit34){
                    wiegandBit = Constants.WiegandBit.WIEGAND34;
                    binding.input.setFilters(new InputFilter[] { new InputFilter.LengthFilter(8)});
                    binding.input.setText("00000001");
                }

                initWiegand(wiegandBit);
            }
        });


        binding.clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.textView.setText("");
            }
        });

        binding.send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                send();
            }
        });

        initWiegand(wiegandBit);
        myTask = new MyTask();
        Timer timer = new Timer();
        timer.schedule(myTask, 1000, 2000);
    }

    private MyTask myTask;
    private class MyTask extends TimerTask{
        @Override
        public void run() {
            rs485Manager.send(new byte[]{0x01, 0x02, 0x03, 0x04});
        }
    }


    private void initWiegand(Constants.WiegandBit wiegandBit){
//        if(wiegandManager != null){
//            wiegandManager.close();
//            SystemClock.sleep(100);
//        }
//
//        wiegandManager.open(wiegandBit, new IOCallBack() {
//            @Override
//            public void onReceiverData(byte[] by) {
//                appendMessage(DataConversion.toHexString(by, by.length));
//            }
//        });
    }

    private void send(){
        String txt = binding.input.getText().toString();
        if (wiegandBit == Constants.WiegandBit.WIEGAND26) {
            if (txt.length() != 6) {
                appendMessage("发送错误 26位 只支持3字节收发");
                return;
            }
        } else if (wiegandBit == Constants.WiegandBit.WIEGAND34) {
            if (txt.length() != 8) {
                appendMessage("发送错误 34位 只支持4字节收发");
                return;
            }
        }

        wiegandManager.send(DataConversion.toByteArray(txt));
    }

    public void appendMessage(String s){
        handler.post(new Runnable() {
            @Override
            public void run() {
                binding.textView.append(s);
                binding.textView.append("\n");
            }
        });

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                binding.scrollView.fullScroll(ScrollView.FOCUS_DOWN);
            }
        }, 100);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        wiegandManager.close();
        myTask.cancel();
        rs485Manager.close();
    }
}
